#include "SpriteMaker.h"

SpriteMaker::SpriteMaker()
{
}


SpriteMaker::~SpriteMaker()
{
}

Sprite* SpriteMaker::makeSpriteWithAnimation(SpriteBatchNode* _Spritebatch, SpriteFrameCache* _Cache, std::string _Name,int _First, int _Number, int _Speed)
{
	std::string name = _Name + "_" + std::to_string(_First / 10) + std::to_string(_First % 10) + ".png";

	Sprite *sprite = Sprite::createWithSpriteFrameName(name);
	_Spritebatch->addChild(sprite);

	Vector <SpriteFrame*> animFrames(_Number);

	char str[100] = { 0 };
	for (int i = _First; i < _Number; i++)
	{
		name = _Name +"_" + "%02d.png";
		sprintf(str, name.c_str(), i);
		SpriteFrame* frame = _Cache->getSpriteFrameByName(str);
		animFrames.pushBack(frame);
	}

	Animation* animation = Animation::createWithSpriteFrames(animFrames, _Speed);
	sprite->runAction(RepeatForever::create(Animate::create(animation)));
	return sprite;
	
}

Sprite* SpriteMaker::makeSprite(SpriteBatchNode* _Spritebatch, std::string _Name)
{
	Sprite *sprite = Sprite::createWithSpriteFrameName(_Name);
	_Spritebatch->addChild(sprite);
	return sprite;
}

Action* SpriteMaker::makeAnimation(SpriteFrameCache* _Cache, std::string _Name, int _First, int _Number, int _Speed)
{
	std::string name = _Name + "_" + std::to_string(_First / 10) + std::to_string(_First % 10) + ".png";
	Vector <SpriteFrame*> animFrames(_Number);

	char str[100] = { 0 };
	for (int i = _First; i < _Number; i++)
	{
		name = _Name + "_" + "%02d.png";
		sprintf(str, name.c_str(), i);
		SpriteFrame* frame = _Cache->getSpriteFrameByName(str);
		animFrames.pushBack(frame);
	}

	Animation* animation = Animation::createWithSpriteFrames(animFrames, _Speed);
	return RepeatForever::create(Animate::create(animation));
}

